package com.yzc35326.typeapp

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import java.io.BufferedReader
import java.lang.StringBuilder

class ItemAdapter(val file:BufferedReader?,val context: Context, val contentList:List<String>,path:String): RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
    private val resultList:List<String> = ArrayList(contentList.size)

    private val array:List<String>?
    init{

        array = file?.readLines()
    }

    inner class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        val content=itemView.findViewById<TextView>(R.id.tv_rv_content)
        val result=itemView.findViewById<TextView>(R.id.tv_rv_result)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item,parent,false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.content.text=contentList[position]
        val sb=StringBuilder()
        if(array==null){

        }else{
            for(j in contentList[position].indices){
                sb.append("[")
                for(i in array.indices){
                    if(array[i][0]==contentList[position][j]){
                        sb.append(array[i].substring(2 until array[i].length))
                        sb.append(" ")
                    }
                }
                sb.append("] ")
            }
        }


        holder.result.text=sb
    }

    override fun getItemCount(): Int {
        return contentList.size
    }
}